package com.jdy.haoduoai.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoai.adapter.MyLoveStoryAdapter;
import com.jdy.haoduoai.adapter.MyPagerAdapter;
import com.jdy.haoduoai.enums.SelectionMode;
import com.jdy.haoduoai.intface.IAsyncTask;
import com.jdy.haoduoai.model.LeFavoriteItem;
import com.jdy.haoduoai.model.LeFavoriteItemWrapper;
import com.jdy.haoduoai.model.LeUser;
import com.jdy.haoduoai.parse.JsonSerializer;
import com.jdy.haoduoai.pulltorefresh.PullToRefreshLayout;
import com.jdy.haoduoai.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.jdy.haoduoai.pulltorefresh.PullableListView;
import com.jdy.haoduoai.util.CustomAsyncTask;
import com.jdy.haoduoai.util.HttpUtils;
import com.jdy.haoduoai.util.ResponseResult;
import com.jdy.haoduoai.util.Tools;
import com.jdy.haoduoai.view.RippleView;
import com.jdy.haoduoai.view.RippleView.OnRippleCompleteListener;
import com.jdy.haoduoai.widget.CustomCommonDialog;
import com.jdy.haoduoai.widget.MyViewPager;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//import com.jdy.haoduoai.R;

/**
 * 我的收藏
 * 
 * @author hf
 * 
 */
@SuppressLint({ "InflateParams", "HandlerLeak" })
public class MyLoveListActivity extends BaseActivity implements
    OnClickListener, OnPageChangeListener {
  @InjectView(id = R.id.select_cartoon, click = true)
  private RelativeLayout select_cartoon;
  @InjectView(id = R.id.tv_cartoon)
  private TextView tv_cartoon;
  @InjectView(id = R.id.select_radio, click = true)
  private RelativeLayout select_radio;
  @InjectView(id = R.id.tv_radio)
  private TextView tv_radio;
  @InjectView(id = R.id.beacon)
  private ImageView beacon;
  @InjectView(id = R.id.vpager)
  private MyViewPager vpager;
  @InjectView(id = R.id.bottomlyt)
  private LinearLayout bottomlyts;
  @InjectView(id = R.id.delete, click = true)
  private Button delete;
  @InjectView(id = R.id.select, click = true)
  private Button select;
  @InjectView(id = R.id.toplyt)
  private LinearLayout toplyt;
  @InjectView(id = R.id.bealyt)
  private LinearLayout bealyt;

  private View page1;
  private View page2;
  private LayoutInflater mInflater;
  private int offset = 0;
  private int currIndex = 0;
  private List<View> views;
  private DisplayMetrics dm = null;
  private int screenW;
  private LayoutParams params;

  // page1
  private LinearLayout emptylayout1;
  private RippleView reloadRippleView1;
  private PullToRefreshLayout refreshLayout1;
  private ListView listView1;
  private ImageView img_defaultfail1;
  private Button btnReload1;

  // page2
  private LinearLayout emptylayout2;
  private RippleView reloadRippleView2;
  private PullToRefreshLayout refreshLayout2;
  private PullableListView listView2;
  private ImageView img_defaultfail2;
  private Button btnReload2;

  private MyLoveStoryAdapter mStoryAdapter;
  private MyLoveStoryAdapter mRadioAdapter;

  private LeFavoriteItemWrapper mLeFavoriteItemWrapper1,
      mLeFavoriteItemWrapper2;

  private ArrayList<LeFavoriteItem> mStoryAdapterList;
  private ArrayList<LeFavoriteItem> mStoryArrayList;

  private ArrayList<LeFavoriteItem> mRadioAdapterList;
  private ArrayList<LeFavoriteItem> mRadioArrayList;

  private LeUser mLeUser;
  private int mLastpage = 1;
  private int mLastpage2 = 1;
  public boolean isrefreshlist1 = false;
  public boolean isrefreshlist2 = false;

  @Override
  @SuppressLint("InflateParams")
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.mylovelist);
    setTitle("我的收藏");

    initUpRightBtn();

    initialize();
    initHandler();
    initListener();
    InitViewPager();
    loadStoryData();
  }

  @SuppressWarnings("unused")
  private void deleteFavorite(final LeFavoriteItem item) {
    new CustomAsyncTask(this, new IAsyncTask() {
      @Override
      public void onRecieveData(Activity activity, ResponseResult result) {
        if (result.isSuccess()) {
          toastShow("删除成功");
          bottomlyts.setVisibility(View.GONE);
          vpager.setScanScroll(false);
          if (currIndex == 0 && mStoryAdapter != null) {
            mStoryAdapter.setEditor(false);
            mStoryAdapter.notifyDataSetChanged();
          } else if (currIndex == 1 && mRadioAdapter != null) {
            mRadioAdapter.setEditor(false);
            mRadioAdapter.notifyDataSetChanged();
          }

//          toplyt.setVisibility(View.VISIBLE);
//          bealyt.setVisibility(View.VISIBLE);
          getbtn_right().setVisibility(View.GONE);
          mBackImageButton.setVisibility(View.VISIBLE);
          mLastpage = 1;
          isrefreshlist1 = true;
          isrefreshlist2 = true;
          loadStoryData();
        } else {
          toastShow(result.data);
        }
      }

      @Override
      public ResponseResult doInbackground(Activity activity) {
        HashMap<String, Object> param_ = new HashMap<String, Object>();
        param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
        return HttpUtils.startRequest(HttpUtils.HOST_API
            + "/api/2.0/member/favorite/" + item.id, param_,
            HttpUtils.TAG_DELETE_REQUEST);
      }
    }).execute();
  }

  private void fillLoveStoryList(boolean loadFail) {
    if (mStoryAdapter == null) {
      if (mStoryArrayList != null && !mStoryArrayList.isEmpty()) {
        mStoryAdapter = new MyLoveStoryAdapter(this, mStoryArrayList);
      } else {
        mStoryAdapter = new MyLoveStoryAdapter(this,
            new ArrayList<LeFavoriteItem>());
      }

      listView1.setAdapter(mStoryAdapter);
    } else {
      mStoryAdapter.notifyDataSetChanged();

      if (mStoryArrayList != null
          && mStoryArrayList.size() == mLeFavoriteItemWrapper1.total) {
        refreshLayout1.refreshFinish(PullToRefreshLayout.LOAD_COMPLETED);
        refreshLayout1.loadmoreFinish(PullToRefreshLayout.LOAD_COMPLETED);
      } else {
        refreshLayout1.refreshFinish(PullToRefreshLayout.SUCCEED);
        refreshLayout1.loadmoreFinish(PullToRefreshLayout.SUCCEED);
      }

      // refreshLayout1.refreshFinish(PullToRefreshLayout.SUCCEED);
      // refreshLayout1.loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }

    if (loadFail) {
      emptylayout1.setVisibility(View.VISIBLE);
      btnReload1.setVisibility(View.VISIBLE);
      img_defaultfail1.setBackgroundResource(R.drawable.defailt_fail);
    } else {
      if (mStoryAdapter.getCount() == 0) {
        emptylayout1.setVisibility(View.VISIBLE);
        btnReload1.setVisibility(View.GONE);
        img_defaultfail1.setBackgroundResource(R.drawable.empty_mylove);
      } else {
        emptylayout1.setVisibility(View.GONE);
      }
    }
  }

  private void fillLoveRadioList(boolean loadFail) {
    if (mRadioAdapter == null) {
      if (mRadioArrayList != null && !mRadioArrayList.isEmpty()) {
        mRadioAdapter = new MyLoveStoryAdapter(this, mRadioArrayList);
      } else {
        mRadioAdapter = new MyLoveStoryAdapter(this,
            new ArrayList<LeFavoriteItem>());
      }

      listView2.setAdapter(mRadioAdapter);
    } else {
      mRadioAdapter.notifyDataSetChanged();

      if (mRadioArrayList != null
          && mRadioArrayList.size() == mLeFavoriteItemWrapper2.total) {
        refreshLayout2.refreshFinish(PullToRefreshLayout.LOAD_COMPLETED);
        refreshLayout2.loadmoreFinish(PullToRefreshLayout.LOAD_COMPLETED);
      } else {
        refreshLayout2.refreshFinish(PullToRefreshLayout.SUCCEED);
        refreshLayout2.loadmoreFinish(PullToRefreshLayout.SUCCEED);
      }

      // refreshLayout2.refreshFinish(PullToRefreshLayout.SUCCEED);
      // refreshLayout2.loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }

    if (loadFail) {
      emptylayout2.setVisibility(View.VISIBLE);
      btnReload2.setVisibility(View.VISIBLE);
      img_defaultfail2.setBackgroundResource(R.drawable.defailt_fail);
    } else {
      if (mRadioAdapter.getCount() == 0) {
        emptylayout2.setVisibility(View.VISIBLE);
        btnReload2.setVisibility(View.GONE);
        img_defaultfail2.setBackgroundResource(R.drawable.empty_mylove);
      } else {
        emptylayout2.setVisibility(View.GONE);
      }
    }
  }

  private void loadStoryData() {
    new CustomAsyncTask(this, new IAsyncTask() {
      @Override
      public void onRecieveData(Activity activity, ResponseResult result) {
        if (result.isSuccess() && Tools.isNotNullStr(result.data)) {
          if (mStoryAdapterList != null) {
            mStoryAdapterList.clear();
          }
          if (isrefreshlist1) {
            mStoryArrayList.clear();
          }

          mLeFavoriteItemWrapper1 = JsonSerializer.getInstance().deserialize(
              result.data, LeFavoriteItemWrapper.class);

          mStoryAdapterList = (ArrayList<LeFavoriteItem>) mLeFavoriteItemWrapper1.data;
          if (Tools.isNotEmpty(mStoryAdapterList)) {
            mStoryArrayList.addAll(mStoryAdapterList);
          }

          fillLoveStoryList(false);
        } else {
          mStoryArrayList.clear();
          fillLoveStoryList(true);
        }

        loadRadioData();
      }

      @Override
      public ResponseResult doInbackground(Activity activity) {
        HashMap<String, Object> param_ = new HashMap<String, Object>();
        param_.put(HttpUtils.TAG_PAGE_I, mLastpage);
        param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
        param_.put("category", "cartoon");
        return HttpUtils.startRequest(HttpUtils.API_URL
            + HttpUtils.GET_FAVORITES_METHOD_GET, param_,
            HttpUtils.TAG_GET_REQUEST);
      }
    }).execute();
  }

  private void loadRadioData() {
    new CustomAsyncTask(this, new IAsyncTask() {
      @Override
      public void onRecieveData(Activity activity, ResponseResult result) {
        if (result.isSuccess() && Tools.isNotNullStr(result.data)) {
          if (mRadioAdapterList != null) {
            mRadioAdapterList.clear();
          }
          if (isrefreshlist2) {
            mRadioArrayList.clear();
          }

          mLeFavoriteItemWrapper2 = JsonSerializer.getInstance().deserialize(
              result.data, LeFavoriteItemWrapper.class);
          mRadioAdapterList = (ArrayList<LeFavoriteItem>) mLeFavoriteItemWrapper2.data;
          if (Tools.isNotEmpty(mRadioAdapterList)) {
            mRadioArrayList.addAll(mRadioAdapterList);
          }

          fillLoveRadioList(false);
        } else {
          mRadioArrayList.clear();
          fillLoveRadioList(true);
        }

        isrefreshlist2 = false;
      }

      @Override
      public ResponseResult doInbackground(Activity activity) {
        HashMap<String, Object> param_ = new HashMap<String, Object>();
        param_.put(HttpUtils.TAG_PAGE_I, mLastpage2);
        param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
        param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
        param_.put("category", "radio");
        return HttpUtils.startRequest(HttpUtils.API_URL
            + HttpUtils.GET_FAVORITES_METHOD_GET, param_,
            HttpUtils.TAG_GET_REQUEST);
      }
    }).execute();
  }

  private void initialize() {
    mLeUser = LeXiaoXiaoBanApp.getInstance().getUser();

    mStoryAdapterList = new ArrayList<>();
    mRadioAdapterList = new ArrayList<>();

    mStoryArrayList = new ArrayList<>();
    mRadioArrayList = new ArrayList<>();

    dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm); // 得到 屏幕的宽度
    screenW = dm.widthPixels;
    params = beacon.getLayoutParams();
    params.width = screenW / 2; // 设置 控件 cursor 图片的 宽度 是 屏宽的 2/5
    beacon.setLayoutParams(params);
    views = new ArrayList<View>(); // 布局集合
    mInflater = LayoutInflater.from(this);

    page1 = mInflater.inflate(R.layout.pagelayout3, null);
    emptylayout1 = (LinearLayout) page1.findViewById(R.id.emptylayout);
    reloadRippleView1 = (RippleView) page1.findViewById(R.id.reloadRippleView);
    refreshLayout1 = (PullToRefreshLayout) page1
        .findViewById(R.id.refresh_view);

    img_defaultfail1 = (ImageView) page1.findViewById(R.id.img_defaultfail);
    btnReload1 = (Button) page1.findViewById(R.id.reload);
    listView1 = (ListView) page1.findViewById(R.id.content_views);

    page2 = mInflater.inflate(R.layout.pagelayout4, null);
    emptylayout2 = (LinearLayout) page2.findViewById(R.id.emptylayout);
    reloadRippleView2 = (RippleView) page2.findViewById(R.id.reloadRippleView);
    refreshLayout2 = (PullToRefreshLayout) page2
        .findViewById(R.id.refresh_view);
    listView2 = (PullableListView) page2.findViewById(R.id.content_views);
    img_defaultfail2 = (ImageView) page2.findViewById(R.id.img_defaultfail);
    btnReload2 = (Button) page2.findViewById(R.id.reload);

    views.add(page1);
    views.add(page2);

    offset = screenW / 2;
  }

  private void InitViewPager() {
    vpager.setAdapter(new MyPagerAdapter(views));
    vpager.setScanScroll(false);
    vpager.setCurrentItem(1);
  }

  private void initHandler() {
    handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
      }
    };
  }

  private void initListener() {
    vpager.setOnPageChangeListener(this);
    refreshLayout1.setOnRefreshListener(new OnRefreshListener() {
      @Override
      public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        mLastpage = 1;
        isrefreshlist1 = true;
        loadStoryData();
      }

      @Override
      public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mLastpage++;
        isrefreshlist1 = false;

        loadStoryData();
      }
    });

    refreshLayout2.setOnRefreshListener(new OnRefreshListener() {
      @Override
      public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        mLastpage2 = 1;
        isrefreshlist2 = true;
        loadRadioData();
      }

      @Override
      public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mLastpage2++;
        isrefreshlist2 = false;
        loadRadioData();
      }
    });

    reloadRippleView1
        .setOnRippleCompleteListener(new OnRippleCompleteListener() {
          @Override
          public void onComplete(RippleView rippleView) {
            mLastpage = 1;
            isrefreshlist1 = true;
            loadStoryData();
          }
        });

    reloadRippleView2
        .setOnRippleCompleteListener(new OnRippleCompleteListener() {
          @Override
          public void onComplete(RippleView rippleView) {
            mLastpage2 = 1;
            isrefreshlist2 = true;
            loadRadioData();
          }
        });

    listView1.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        Intent intent = new Intent();
        intent.setClass(MyLoveListActivity.this, StoryDetailActivity.class);
        intent.putExtra("id", mStoryArrayList.get(arg2).episode.album_id);
        intent.putExtra("sort", mStoryArrayList.get(arg2).episode.sort); // 第几集，高亮
        startActivity(intent);
      }
    });

    listView2.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        Intent intent = new Intent();
        intent.setClass(MyLoveListActivity.this, StoryDetailActivity.class);
        intent.putExtra("id", mRadioArrayList.get(arg2).episode.album_id);
        intent.putExtra("sort", mRadioArrayList.get(arg2).episode.sort); // 第几集，高亮
        startActivity(intent);
      }
    });
  }

  private void initUpRightBtn() {
    getbtn_right().setVisibility(View.VISIBLE);
    getbtn_right().setBackgroundResource(R.drawable.edit_btn);
    getbtn_right().setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (!bottomlyts.isShown()) {
          bottomlyts.setVisibility(View.VISIBLE);
          getbtn_right().setBackgroundResource(R.drawable.cancle);
          vpager.setScanScroll(false);
          if (currIndex == 0 && mStoryAdapter != null) {
            mStoryAdapter.setEditor(true);
            mStoryAdapter.notifyDataSetChanged();
          } else if (currIndex == 1 && mRadioAdapter != null) {
            mRadioAdapter.setEditor(true);
            mRadioAdapter.notifyDataSetChanged();
          }

          toplyt.setVisibility(View.GONE);
          bealyt.setVisibility(View.GONE);
          mBackImageButton.setVisibility(View.INVISIBLE);
        } else {
          bottomlyts.setVisibility(View.GONE);
          vpager.setScanScroll(false);
          if (currIndex == 0 && mStoryAdapter != null) {
            mStoryAdapter.setEditor(false);
            mStoryAdapter.notifyDataSetChanged();

            if (mStoryArrayList != null) {
              for (LeFavoriteItem item : mStoryArrayList) {
                item.isChecked = false;
              }
            }
          } else if (currIndex == 1 && mRadioAdapter != null) {
            mRadioAdapter.setEditor(false);

            if (mRadioArrayList != null) {
              for (LeFavoriteItem item : mRadioArrayList) {
                item.isChecked = false;
              }
            }

            mRadioAdapter.notifyDataSetChanged();
          }

//          toplyt.setVisibility(View.VISIBLE);
//          bealyt.setVisibility(View.VISIBLE);
          getbtn_right().setBackgroundResource(R.drawable.edit_btn);
          mBackImageButton.setVisibility(View.VISIBLE);
        }
      }
    });
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
//    case R.id.select_cartoon:
//      vpager.setCurrentItem(0);
//      break;
    case R.id.select_radio:
      vpager.setCurrentItem(1);
      break;
    case R.id.delete:

      if (currIndex == 0) {
        deleteStory();
      } else {
        deleteRadio();
      }
      break;
    case R.id.select:
      if (select.getText().toString().equals("全选")) {
        if (currIndex == 0 && mStoryAdapter != null) {
          mStoryAdapter.setSelectMode(SelectionMode.SELECT_ALL);
          mStoryAdapter.notifyDataSetChanged();
        } else if (currIndex == 1 && mRadioAdapter != null) {
          mRadioAdapter.setSelectMode(SelectionMode.SELECT_ALL);
          mRadioAdapter.notifyDataSetChanged();
        }

        select.setText("取消全选");
      } else if (select.getText().toString().equals("取消全选")) {
        if (currIndex == 0 & mStoryAdapter != null) {
          mStoryAdapter.setSelectMode(SelectionMode.DESELECT_ALL);
          mStoryAdapter.notifyDataSetChanged();
        } else if (currIndex == 1 && mRadioAdapter != null) {
          mRadioAdapter.setSelectMode(SelectionMode.DESELECT_ALL);
          mRadioAdapter.notifyDataSetChanged();
        }

        select.setText("全选");
      }
      break;
    default:
      break;
    }
  }

  private void deleteRadio() {
    int selectCount = 0;
    if (mRadioArrayList != null) {
      for (LeFavoriteItem item : mRadioArrayList) {
        if (item != null && item.isChecked) {
          ++selectCount;
        }
      }
    }

    if (selectCount > 0) {
      CustomCommonDialog.Builder cdb = new CustomCommonDialog.Builder(
          MyLoveListActivity.this).setMessage("确定删除吗？").setTitle("删除提示")
          .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
            }
          }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

              if (mRadioArrayList != null && !mRadioArrayList.isEmpty()) {
                final List<LeFavoriteItem> selectedRecordings = new ArrayList<LeFavoriteItem>();
                String storyIds = "";

                for (LeFavoriteItem item : mRadioArrayList) {
                  if (item.isChecked) {
                    selectedRecordings.add(item);
                    storyIds += item.id;
                    storyIds += ";";
                  }
                }

                if (Tools.isNotEmpty(storyIds)) {
                  storyIds = storyIds.substring(0, storyIds.length() - 1);
                }

                if (selectedRecordings.size() > 0) {
                  showloading("");
                  final String finalStoryIds = storyIds;
                  new CustomAsyncTask(MyLoveListActivity.this,
                      new IAsyncTask() {
                        @Override
                        public void onRecieveData(Activity activity,
                            ResponseResult result) {

                          // if (result != null && result.isSuccess()) {
                          for (LeFavoriteItem rItem : selectedRecordings) {
                            mRadioAdapter.remove(rItem);
                          }
                          mRadioAdapter.removeSelection();

                          if (Tools.isNotNullStr(result.data)) {
                            Toast.makeText(MyLoveListActivity.this,
                                result.data, Toast.LENGTH_SHORT).show();
                          }
                          // }

                          dismissLoading();
                        }

                        @Override
                        public ResponseResult doInbackground(Activity activity) {
                          if (mLeUser == null) {
                            mLeUser = Tools.restoreLeUser();
                          }

                          HashMap<String, Object> param_ = new HashMap<String, Object>();
                          param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
                          param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                          return HttpUtils.startRequest(HttpUtils.HOST_API
                              + "/api/2.0/member/favorite/" + finalStoryIds,
                              param_, HttpUtils.TAG_DELETE_REQUEST);
                        }
                      }).execute();

                }
                dialog.dismiss();
              }
            }
          });
      cdb.create().show();
    } else {
      toastShow("未选中任何条目!");
    }
  }

  private void deleteStory() {
    int selectCount = 0;
    if (mStoryArrayList != null) {
      for (LeFavoriteItem item : mStoryArrayList) {
        if (item != null && item.isChecked) {
          ++selectCount;
        }
      }
    }

    if (selectCount > 0) {
      CustomCommonDialog.Builder cdb = new CustomCommonDialog.Builder(
          MyLoveListActivity.this).setMessage("确定删除吗？").setTitle("删除提示")
          .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
            }
          }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

              if (mStoryArrayList != null && !mStoryArrayList.isEmpty()) {
                final List<LeFavoriteItem> selectedRecordings = new ArrayList<LeFavoriteItem>();
                String storyIds = "";

                for (LeFavoriteItem item : mStoryArrayList) {
                  if (item.isChecked) {
                    selectedRecordings.add(item);
                    storyIds += item.id;
                    storyIds += ";";
                  }
                }

                if (Tools.isNotEmpty(storyIds)) {
                  storyIds = storyIds.substring(0, storyIds.length() - 1);
                }

                if (selectedRecordings.size() > 0) {
                  showloading("");
                  final String finalStoryIds = storyIds;
                  new CustomAsyncTask(MyLoveListActivity.this,
                      new IAsyncTask() {
                        @Override
                        public void onRecieveData(Activity activity,
                            ResponseResult result) {

                          dismissLoading();
                          // if (result != null && result.isSuccess()) {
                          for (LeFavoriteItem rItem : selectedRecordings) {
                            mStoryAdapter.remove(rItem);
                          }
                          mStoryAdapter.removeSelection();

                          if (Tools.isNotNullStr(result.data)) {
                            Toast.makeText(MyLoveListActivity.this,
                                result.data, Toast.LENGTH_SHORT).show();
                          }
                          // }
                        }

                        @Override
                        public ResponseResult doInbackground(Activity activity) {
                          if (mLeUser == null) {
                            mLeUser = Tools.restoreLeUser();
                          }

                          HashMap<String, Object> param_ = new HashMap<String, Object>();
                          param_.put(HttpUtils.TAG_SNO_I, mLeUser.sno);
                          param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);
                          return HttpUtils.startRequest(HttpUtils.HOST_API
                              + "/api/2.0/member/favorite/" + finalStoryIds,
                              param_, HttpUtils.TAG_DELETE_REQUEST);
                        }
                      }).execute();

                }
                dialog.dismiss();
              }
            }
          });
      cdb.create().show();
    } else {
      toastShow("未选中任何条目!");
    }
  }

  @Override
  public void onPageScrollStateChanged(int arg0) {
  }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
  }

  @Override
  public void onPageSelected(int arg0) {
    Animation animation = null;
    switch (arg0) {
    case 0:
      if (currIndex == 1) {
        animation = new TranslateAnimation(offset, 0, 0, 0);
      }

      tv_cartoon.setTextColor(this.getResources().getColor(R.color.litread));
      tv_radio.setTextColor(this.getResources().getColor(R.color.black));

      break;
    case 1:
      if (currIndex == 0) {
        animation = new TranslateAnimation(0, offset, 0, 0);
      }
      tv_cartoon.setTextColor(this.getResources().getColor(R.color.black));
      tv_radio.setTextColor(this.getResources().getColor(R.color.litread));

      break;
    }
    currIndex = arg0;

    if (animation != null) {
      animation.setFillAfter(true);
      animation.setDuration(200);
      beacon.startAnimation(animation);
    }
  }

}
